<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-form-item label="名称" name="name">
						<a-input v-model:value="searchFormState.name" placeholder="请输入名称" />
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="类型" name="type">
						<a-select
							v-model:value="searchFormState.type"
							:options="courseTypeOptions"
							style="width: 100%"
							placeholder="请选择类型"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="3">
					<a-form-item label="学分" name="credit">
						<a-input-number v-model:value="searchFormState.credit" min="1" placeholder="请输入" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('eduCourseAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<a-button @click="openAssignForm()">
						<template #icon><export-outlined /></template>
						批量配课
					</a-button>
					<a-button @click="batchOffer">
						<template #icon><export-outlined /></template>
						直接开课
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('eduCourseBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchEduCourse"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'studentType'">
					{{ $TOOL.dictTypeData('STUDENT_TYPE', record.studentType) }}
				</template>
				<template v-if="column.dataIndex === 'type'">
					{{ $TOOL.dictTypeData('COURSE_TYPE', record.type) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('eduCourseEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['eduCourseEdit', 'eduCourseDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteEduCourse(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('eduCourseDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<AssignForm ref="assignFormRef" @submit="submitAssignForm" />
</template>

<script setup name="course">
	import Form from './form.vue'
	import AssignForm from './assignForm.vue'
	import eduCourseApi from '@/api/biz/eduCourseApi'
	import tool from '@/utils/tool'
	import { message } from 'ant-design-vue'
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const assignFormRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '名称',
			dataIndex: 'name'
		},
		{
			title: '学分',
			dataIndex: 'credit'
		},
		{
			title: '总学时',
			dataIndex: 'classHour'
		},
		{
			title: '类型',
			dataIndex: 'type'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['eduCourseEdit', 'eduCourseDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	const selectedRowKeys = ref([])
	// 学年分类字典
	const yearOptions = tool.dictList('YEAR')
	// 课程类型字典
	const courseTypeOptions = tool.dictList('COURSE_TYPE')
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return eduCourseApi.eduCoursePage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 删除
	const deleteEduCourse = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		eduCourseApi.eduCourseDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	const submitAssignForm = (major, studentType, grade) => {
		// 处理传递的值
		console.log(major, studentType, grade)
		if (selectedRowKeys.value.length < 1) {
			message.warning('请输入查询条件或勾选要导出的信息')
		}
		if (selectedRowKeys.value.length > 0) {
			const params = {
				major: major,
				studentType: studentType,
				grade: grade,
				courseIds: selectedRowKeys.value
					.map((m) => {
						return m
					})
					.join()
			}
			exportBatchCourse(params)
			return
		}
	}
	// 批量导出
	const exportBatchCourse = (params) => {
		eduCourseApi.eduCourseExport(params).then(() => {
			message.success('操作成功')
			table.value.clearSelected()
		})
	}
	// 打开配课参数窗口
	const openAssignForm = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请勾选要配课的记录')
			return
		}
		const params = {
			courseIds: selectedRowKeys.value
				.map((m) => {
					return m
				})
				.join()
		}
		assignFormRef.value.onOpen(params)
	}

	// 一键配课
	const smartExport = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请勾选需要配课的课程')
			return
		}
		let params = {
			courseIds: selectedRowKeys.value
				.map((m) => {
					return m
				})
				.join()
		}
		smartExportCourse(params)
		return
	}
	const smartExportCourse = (params) => {
		eduCourseApi.eduCourseSmartExport(params).then(() => {
			message.success('操作成功')
			table.value.clearSelected()
		})
	}

	// 批量删除
	const deleteBatchEduCourse = (params) => {
		eduCourseApi.eduCourseDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
	// 批量开课
	const batchOffer = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择要开课的记录')
		}
		if (selectedRowKeys.value.length > 0) {
			const params = {
				courseIds: selectedRowKeys.value
					.map((m) => {
						return m
					})
					.join()
			}
			offer(params)
			return
		}
	}
	// 批量开课
	const offer = (params) => {
		eduCourseApi.eduCourseOffer(params).then(() => {
			message.success('操作成功')
			table.value.clearSelected()
		})
	}
</script>
